package com.struts.deal.user.register;

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.SQLException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import com.ibatis.po.user.*;
import com.ibatis.po.system.*;
import com.base.BaseLog;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.lg.system.CDBManager;
import com.lg.system.CSqlMap;
import com.lg.system.Common;

import com.struts.web.user.register.FavoriteForJobForm;

public class FavoriteDeal extends BaseLog{
	/**
	 * 设置默认信息
	 * @param request
	 * @param response
	 * @param aWebForm
	 * @throws SQLException
	 */
	public static  void setDefault(HttpServletRequest request,
            HttpServletResponse response,FavoriteForJobForm aWebForm) throws  SQLException{
		SqlMapClient sqlmap = CSqlMap.getSqlMapConfig();
		String str="";
    	String str2="";
    	String str3="";
        String sKey="";
        String sValue="";
        List vocationList = null;
        List stationList = null;
        List substationList = null;
        List addressList = null;
        TVocation vocation = new TVocation();
        TStation  station = new TStation();
        TCiyt city= new TCiyt();
        
        str2 += "var station=new Array('','－－请选择－－'";
        
    	try{
        	vocationList = sqlmap.queryForList("T_VOCATION.selectAll");
        	if(vocationList.size()>0){
        		vocation=(TVocation)vocationList.get(0);
        		str += "var vocation=new Array('"+vocation.getId()+"','"+vocation.getVocationName()+"'";
        	
        	for(int i=1;i<vocationList.size();i++){
        		vocation=(TVocation)vocationList.get(i);
        		str+=",'"+vocation.getId()+"',";
        		str+="'"+vocation.getVocationName()+"'\n";
        	}
        	str+=");\n";
        	}
        	
        	
        	stationList = sqlmap.queryForList("T_STATION.selectAll");
        	int length  = stationList.size();
        	for(int i=0;i<stationList.size();i++){
        		station=(TStation)stationList.get(i);
        		str2+=",'"+station.getId()+"',";
        		str2+="'"+station.getSortName()+"'\n";
        	}
        	str2+=");\n";
        	if(length>0){
        	str2+="var substation=new Array("+length+");\n";
        	str2+="substation[0]=new Array('','');\n";
        	for(int i=0;i<stationList.size();i++){
        	station=(TStation)stationList.get(i);
        	sKey = station.getId();
        	TSubStation substationPo = new TSubStation();
        	substationPo.setSortId(sKey);
        	substationList = sqlmap.queryForList("T_SUB_STATION.selectBySortId",substationPo);
        	if(substationList.size()>0){
        		substationPo = (TSubStation)substationList.get(0);
        	
        	str2+="substation["+(i+1)+"]=new Array('"+substationPo.getId()+"','"+substationPo.getPostName()+"'\n";  
        	}
        	  for(int j=1;j<substationList.size();j++){
        		  substationPo=(TSubStation)substationList.get(j);
        		  str2+=",'"+substationPo.getId()+"','"+substationPo.getPostName()+"'\n";
        	  }
        	  str2+=");\n";
        	}
        	}
        	
        	addressList = sqlmap.queryForList("T_CIYT.selectAllCity");
        	if(addressList.size()>0){
        		city=(TCiyt)addressList.get(0);
        		str3 += "var address=new Array('"+city.getId()+"','"+city.getCityName()+"'";
        	for(int i=1;i<addressList.size();i++){
        		city=(TCiyt)addressList.get(i);
        		sKey = city.getId();
            	sValue = city.getCityName();
                if((Integer.parseInt(sKey)%100)==0){
        		str3+=",'"+sKey+"',";
        		str3+="'"+sValue+"'\n";
                }else{
                	str3+=",'"+sKey+"',";
            		str3+="'&nbsp;&nbsp;&nbsp;&nbsp;"+sValue+"'\n";
                }
        	}
        	
        	str3+=");\n";
        	}
        	
        	aWebForm.setPrintVocSql(str);
        	aWebForm.setPrintStaSql(str2);
        	aWebForm.setPrintAddSql(str3);
        	}catch(Exception e){
        		logger.info("验证用户是否存在是发生错误!\n");
        		e.printStackTrace();
        	}
    	
    	
    }

	/**
	 * 添加
	 * @param request
	 * @param response
	 * @param aWebForm
	 * @throws SQLException
	 */
	public static  void doAdd(HttpServletRequest request,
            HttpServletResponse response,FavoriteForJobForm aWebForm) throws  SQLException{
		
		SqlMapClient sqlmap = CSqlMap.getSqlMapConfig();
		TUser user = (TUser)request.getSession().getAttribute("TUser");
        
    	try{
    		if(user!=null){
    			String userid = user.getId();
        	//意向行业
        	if(aWebForm.getVocationList()!=null && aWebForm.getVocationList().length()>0){
    			TFavoriteVocation  fvocationPo = new TFavoriteVocation();
    			String[]  fvocationArray = aWebForm.getVocationList().split(",");
    			fvocationPo.setUserid(userid);
    			sqlmap.delete("T_FAVORITE_VOCATION.deleteByUserId",fvocationPo);
    			for(int i=0;i<fvocationArray.length;i++){
    				fvocationPo.setId(Common.getNewID());
    				fvocationPo.setUserid(userid);
    				fvocationPo.setVocationId(fvocationArray[i]);
    				
    				sqlmap.insert("T_FAVORITE_VOCATION.abatorgenerated_insert",fvocationPo);
    			}
    			
    		}
        	
        	//意向岗位
        	if(aWebForm.getStationList()!=null && aWebForm.getStationList().length()>0){
        		TFavoritePost  fpositionPo = new TFavoritePost();
    			String[]  fpositionArray = aWebForm.getStationList().split(",");
    			fpositionPo.setUserid(userid);
    			sqlmap.delete("T_FAVORITE_POST.deleteByUserId",fpositionPo);
    			for(int i=0;i<fpositionArray.length;i++){
    				fpositionPo.setId(Common.getNewID());
    				fpositionPo.setUserid(userid);
    				fpositionPo.setPostId(fpositionArray[i]);
    				sqlmap.insert("T_FAVORITE_POST.abatorgenerated_insert",fpositionPo);
    			}
    			
    		}
        	
        	//意向地区
        	if(aWebForm.getPlaceList()!=null && aWebForm.getPlaceList().length()>0){
        		TFavoriteAddress  faddressPo = new TFavoriteAddress();
    			String[]  faddressArray = aWebForm.getPlaceList().split(",");
    			faddressPo.setUserId(userid);
    			sqlmap.delete("T_FAVORITE_ADDRESS.deleteByUserId",faddressPo);
    			for(int i=0;i<faddressArray.length;i++){
    				faddressPo.setId(Common.getNewID());
    				faddressPo.setUserId(userid);
    				faddressPo.setCityId(faddressArray[i]);
    				sqlmap.insert("T_FAVORITE_ADDRESS.abatorgenerated_insert",faddressPo);
    			}
    			
    		}
        	
        	TJobRequirement fjobRequirementPo = new TJobRequirement();
        	fjobRequirementPo.setId(Common.getNewID());
        	fjobRequirementPo.setUserId(userid);
        	fjobRequirementPo.setJobType(aWebForm.getJobType());
        	fjobRequirementPo.setMonthlyPay(aWebForm.getMonthlyPay());
        	fjobRequirementPo.setContainEat(aWebForm.getContainEat());
        	fjobRequirementPo.setContainLodging(aWebForm.getContainLodging());
        	logger.info("interview:"+aWebForm.getInterview());
        	fjobRequirementPo.setInterView(aWebForm.getInterview());
        	fjobRequirementPo.setOthersRequirement(aWebForm.getOtherRequire());
        	fjobRequirementPo.setArriveTime(aWebForm.getArriveTime());
        	
        	int count = 0;
        	count = ((Integer)sqlmap.queryForObject("T_JOB_REQUIREMENT.selectByUserId",fjobRequirementPo)).intValue();
        	if(count>0){
        		sqlmap.update("T_JOB_REQUIREMENT.updateByUserId",fjobRequirementPo);
        	}else{
        		sqlmap.insert("T_JOB_REQUIREMENT.abatorgenerated_insert",fjobRequirementPo);
        	}
    		}
        	}catch(Exception e){
        		logger.info("验证用户是否存在是发生错误!\n");
        		e.printStackTrace();
        	}
    	
    	
    }
}
